package sisi.enquires;

import java.util.List;

import javax.persistence.EntityManager;
/**
 * ReqshipfuelassignementController.java - Controller class for the <i>reqshipfuelassignement database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 24/1/2014
 * 
 */
public class ReqshipfuelassignementController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Reqshipfuelassignement[] get(String cerca) {
		List<Reqshipfuelassignement> lista = getList(cerca);
			return lista.toArray(new Reqshipfuelassignement[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Reqshipfuelassignement> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.vessel) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Reqshipfuelassignement as c " + cWhere 
					+ " order by c.ref ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Reqshipfuelassignement> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	public Reqshipfuelassignement getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Reqshipfuelassignement reqshipfuelassignementx = em.find(Reqshipfuelassignement.class, id);
			return reqshipfuelassignementx;
		} finally {
			em.close();
		}
	}
	
	public Reqshipfuelassignement getxIdRef(int idref) {
		EntityManager em = getEntityManager();
		try {
			String cQuery = "select c from Reqshipfuelassignement as c where c.ref="
				+ idref + "";
			
			javax.persistence.Query q = em
					.createQuery(cQuery);
			return ((Reqshipfuelassignement) q.getSingleResult());
		} catch (Exception e) {
			return new Reqshipfuelassignement();
		} finally {
			em.close();
		}

	}

	@SuppressWarnings("finally")
	public Reqshipfuelassignement add(Reqshipfuelassignement reqshipfuelassignement) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(reqshipfuelassignement);
			em.getTransaction().commit();
			reqshipfuelassignement = refresh(reqshipfuelassignement);
		} finally {
			em.close();
			return reqshipfuelassignement;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Reqshipfuelassignement reqshipfuelassignement) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Reqshipfuelassignement reqshipfuelassignementx = em.find(Reqshipfuelassignement.class, reqshipfuelassignement.getId());
			reqshipfuelassignementx.setLocation(reqshipfuelassignement.getLocation());
			reqshipfuelassignementx.setId(reqshipfuelassignement.getId());
			reqshipfuelassignementx.setPort(reqshipfuelassignement.getPort());
			reqshipfuelassignementx.setRef(reqshipfuelassignement.getRef());
			reqshipfuelassignementx.setCountry(reqshipfuelassignement.getCountry());
			reqshipfuelassignementx.setAgent(reqshipfuelassignement.getAgent());
			reqshipfuelassignementx.setDeliverytime(reqshipfuelassignement.getDeliverytime());
			reqshipfuelassignementx.setEtatime(reqshipfuelassignement.getEtatime());
			reqshipfuelassignementx.setEtdtime(reqshipfuelassignement.getEtdtime());
			reqshipfuelassignementx.setLastport(reqshipfuelassignement.getLastport());
			reqshipfuelassignementx.setNextport(reqshipfuelassignement.getNextport());
			reqshipfuelassignementx.setPortid(reqshipfuelassignement.getPortid());
			reqshipfuelassignementx.setAn(reqshipfuelassignement.getAn());
			reqshipfuelassignementx.setBa(reqshipfuelassignement.getBa());
			reqshipfuelassignementx.setBuyer(reqshipfuelassignement.getBuyer());
			reqshipfuelassignementx.setCc1(reqshipfuelassignement.getCc1());
			reqshipfuelassignementx.setCc2(reqshipfuelassignement.getCc2());
			reqshipfuelassignementx.setCc3(reqshipfuelassignement.getCc3());
			reqshipfuelassignementx.setCc4(reqshipfuelassignement.getCc4());
			reqshipfuelassignementx.setDelivery(reqshipfuelassignement.getDelivery());
			reqshipfuelassignementx.setDtdate(reqshipfuelassignement.getDtdate());
			reqshipfuelassignementx.setEta(reqshipfuelassignement.getEta());
			reqshipfuelassignementx.setEtd(reqshipfuelassignement.getEtd());
			reqshipfuelassignementx.setIq1(reqshipfuelassignement.getIq1());
			reqshipfuelassignementx.setIq2(reqshipfuelassignement.getIq2());
			reqshipfuelassignementx.setIq3(reqshipfuelassignement.getIq3());
			reqshipfuelassignementx.setIq4(reqshipfuelassignement.getIq4());
			reqshipfuelassignementx.setLocalphone(reqshipfuelassignement.getLocalphone());
			reqshipfuelassignementx.setMds(reqshipfuelassignement.getMds());
			reqshipfuelassignementx.setQc1(reqshipfuelassignement.getQc1());
			reqshipfuelassignementx.setQc2(reqshipfuelassignement.getQc2());
			reqshipfuelassignementx.setQc3(reqshipfuelassignement.getQc3());
			reqshipfuelassignementx.setQc4(reqshipfuelassignement.getQc4());
			reqshipfuelassignementx.setSc1(reqshipfuelassignement.getSc1());
			reqshipfuelassignementx.setSc2(reqshipfuelassignement.getSc2());
			reqshipfuelassignementx.setSc3(reqshipfuelassignement.getSc3());
			reqshipfuelassignementx.setSc4(reqshipfuelassignement.getSc4());
			reqshipfuelassignementx.setSpecs(reqshipfuelassignement.getSpecs());
			reqshipfuelassignementx.setVessel(reqshipfuelassignement.getVessel());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Reqshipfuelassignement refresh(Reqshipfuelassignement reqshipfuelassignement) {

		Reqshipfuelassignement reqshipfuelassignementx = reqshipfuelassignement;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			reqshipfuelassignementx = em.find(Reqshipfuelassignement.class, reqshipfuelassignement.getId());
			em.flush();
			em.refresh(reqshipfuelassignementx);
		} finally {
			em.close();
			return reqshipfuelassignementx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Reqshipfuelassignement reqshipfuelassignement) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Reqshipfuelassignement reqshipfuelassignementx = em.find(Reqshipfuelassignement.class, reqshipfuelassignement.getId());
			em.remove(reqshipfuelassignementx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//
// fine
